package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.GZMyActivity;
import com.example.administrator.szb.activity.GZXYActivity;
import com.example.administrator.szb.activity.MessageActivity;
import com.example.administrator.szb.activity.MyAnswerActivity;
import com.example.administrator.szb.activity.MyCollectionActivity;
import com.example.administrator.szb.activity.MyGZActivity;
import com.example.administrator.szb.activity.MyHDActivity;
import com.example.administrator.szb.activity.MyXMActivity;
import com.example.administrator.szb.activity.SZActivity;
import com.example.administrator.szb.activity.UserEditActivity;
import com.example.administrator.szb.adapter.AppBarStateChangeListener;
import com.example.administrator.szb.fragments.base.BaseFragment;
import com.example.administrator.szb.bean.UserBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/9/5.
 */

public class FragmentMy extends BaseFragment {

    @Bind(R.id.yh_image_icon)
    CircleImageView yhImageIcon;
    @Bind(R.id.my_yh_username)
    TextView myYhUsername;
    @Bind(R.id.my_text_js)
    TextView myTextJs;
    @Bind(R.id.my_unlogin_fb)
    ImageView myUnloginFb;
    @Bind(R.id.my_unlogin_title)
    TextView myUnloginTitle;
    @Bind(R.id.my_unlogin_toolbar)
    Toolbar myUnloginToolbar;
    @Bind(R.id.my_unlogin_collapsingtoolbarlayout)
    CollapsingToolbarLayout myUnloginCollapsingtoolbarlayout;
    @Bind(R.id.my_unlogin_appbarlayout)
    AppBarLayout myUnloginAppbarlayout;
    @Bind(R.id.my_wdwt_num)
    TextView myWdwtNum;
    @Bind(R.id.my_ll_wdwt)
    LinearLayout myLlWdwt;
    @Bind(R.id.my_wdhd_num)
    TextView myWdhdNum;
    @Bind(R.id.my_ll_wdhd)
    LinearLayout myLlWdhd;
    @Bind(R.id.my_wdgz_num)
    TextView myWdgzNum;
    @Bind(R.id.my_ll_wdgz)
    LinearLayout myLlWdgz;
    @Bind(R.id.my_gzwd_num)
    TextView myGzwdNum;
    @Bind(R.id.my_ll_gzwd)
    LinearLayout myLlGzwd;
    @Bind(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @Bind(R.id.my_ll_wdxm)
    LinearLayout myLlWdxm;
    @Bind(R.id.my_ll_wdsc)
    LinearLayout myLlWdsc;
    @Bind(R.id.my_ll_xxsc)
    LinearLayout myLlXxsc;
    @Bind(R.id.my_ll_gywm)
    LinearLayout myLlGywm;
    @Bind(R.id.my_ll_sz)
    LinearLayout myLlSz;
    @Bind(R.id.my_framelayout)
    FrameLayout myFramelayout;
    @Bind(R.id.fragment_my_swipelayout)
    SwipeRefreshLayout fragmentMineSwiperefreshLayout;
    private View view;
    UserBean userBean;//用户个人中心标签




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, view);
        requestUserData();//获取用户信息
        initRefreshLayout();
        initListener();

        UserEditActivity.isRefresh = new UserEditActivity.isRefresh() {
            @Override
            public void doSomeThing(int is_fresh) {
                requestUserData();//获取用户信息
            }
        };

        return view;
    }

    private void initListener() {
//        /**
//         * 是否显示需要认证
//         */
//        if (isRz == FragmentsFlag.Activity_Home.ordinal()) {
//            relativeLayout.setVisibility(View.GONE);
//        }


        /**
         * 监听appBarLayout的滑动状态
         */
        myUnloginAppbarlayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    myUnloginTitle.setText("");
                    fragmentMineSwiperefreshLayout.setEnabled(true);
                    //展开状态

                } else if (state == State.COLLAPSED) {

                    //折叠状态
                    //myUnloginTitle.setText("我的");
                    fragmentMineSwiperefreshLayout.setEnabled(false);

                } else {

                    //中间状态

                }
            }
        });
    }

    private void initRefreshLayout() {
        fragmentMineSwiperefreshLayout.setColorSchemeResources(R.color.zhutise);
        fragmentMineSwiperefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                fragmentMineSwiperefreshLayout.setRefreshing(true);
                requestUserData();
            }
        });

        fragmentMineSwiperefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fragmentMineSwiperefreshLayout.setRefreshing(true);
                requestUserData();
            }
        });
    }

    /**
     * 用户信息
     */
    public  void requestUserData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        String url = URLAddress.URLBASE + URLAddress.URL_CENTER;
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);

        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, UserBean.class, URLAddress.URLBASE + URLAddress.URL_CENTER, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                userBean = (UserBean) response;
                if (userBean.getResult() == 1) {
                    initView();
                    myFramelayout.setVisibility(View.GONE);
                    fragmentMineSwiperefreshLayout.setRefreshing(false);
                } else {
                   // DialogUtil.showSimpleDialog(getActivity(), userBean.getErr_msg());
                    Toasts.show(getActivity(), ""+userBean.getErr_msg(), Toast.LENGTH_SHORT);
                }
                //response.toString();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                fragmentMineSwiperefreshLayout.setRefreshing(false);
                DialogUtil.showSimpleDialog(getActivity(), error.toString());
            }
        });
    }

    /**
     * 获取数据后刷新页面
     */
    private void initView() {
        if (!TextUtils.isEmpty(userBean.getData().getHeadimg())) {
            Glide.with(getActivity()).load(userBean.getData().getHeadimg()).error(R.drawable.error).into(yhImageIcon);
        }

        if (!TextUtils.isEmpty(userBean.getData().getName())) {
            myYhUsername.setText(userBean.getData().getName());
        } else {
            myYhUsername.setText("无名氏");
        }

        if (!TextUtils.isEmpty((String) userBean.getData().getDescription())) {
            myTextJs.setText((String) userBean.getData().getDescription());
        } else {
            myTextJs.setText("快去编辑您的资料吧！");
        }


        myWdwtNum.setText(userBean.getData().getMy_refer());


        myWdhdNum.setText(userBean.getData().getMy_answer());


        myWdgzNum.setText(userBean.getData().getMy_att());


        myGzwdNum.setText(userBean.getData().getAtt_my());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.my_unlogin_fb, R.id.yh_image_icon, R.id.my_ll_wdwt, R.id.my_ll_wdhd, R.id.my_ll_wdgz, R.id.my_ll_gzwd, R.id.my_ll_wdxm, R.id.my_ll_wdsc, R.id.my_ll_xxsc, R.id.my_ll_gywm, R.id.my_ll_sz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_unlogin_fb:
                startActivity(new Intent(getActivity(), UserEditActivity.class));
                break;
            case R.id.yh_image_icon:
                break;
            case R.id.my_ll_wdwt:
                startActivity(new Intent(getActivity(), MyAnswerActivity.class));
                break;
            case R.id.my_ll_wdhd:
                startActivity(new Intent(getActivity(), MyHDActivity.class));
                break;
            case R.id.my_ll_wdgz:
                startActivity(new Intent(getActivity(), MyGZActivity.class));
                break;
            case R.id.my_ll_gzwd:
                startActivity(new Intent(getActivity(), GZMyActivity.class));
                break;
            case R.id.my_ll_wdxm:
                startActivity(new Intent(getActivity(), MyXMActivity.class));
                break;
            case R.id.my_ll_wdsc:
                startActivity(new Intent(getActivity(), MyCollectionActivity.class));
                break;
            case R.id.my_ll_xxsc:
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.my_ll_gywm:
                Intent intent_web = new Intent(getActivity(), GZXYActivity.class);//额，
                intent_web.putExtra("title", "关于我们");
                intent_web.putExtra("id", 1);
                startActivity(intent_web);
                break;
            case R.id.my_ll_sz:
                startActivity(new Intent(getActivity(), SZActivity.class));
                break;
        }
    }


}
